package com.example.myaccount.activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;

import com.example.myaccount.R;
import com.example.myaccount.adapter.LvMoreAdapter;
import com.example.myaccount.adapter.LvSettingsAdapter;
import com.example.myaccount.adapter.RvFlowAdapter;
import com.example.myaccount.adapter.SyLinearLayoutManager;
import com.example.myaccount.constant.MyConst;
import com.example.myaccount.database.DatabaseHelper;
import com.google.android.material.floatingactionbutton.FloatingActionButton;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.ActionBarDrawerToggle;
import androidx.appcompat.app.AppCompatActivity;

import android.util.Log;
import android.view.View;

import androidx.appcompat.widget.Toolbar;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import android.view.Menu;
import android.view.MenuItem;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.TextView;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;

public class MainActivity extends AppCompatActivity {

    private static Context context;

    private DatabaseHelper databaseHelper;

    private AppBarConfiguration appBarConfiguration;
//    private ActivityMainBinding binding;

    private DrawerLayout mDrawerLayout;
    private ActionBarDrawerToggle mDrawerToggle;
    protected Toolbar mTbMain;
    private ListView mLvSettings,mLvMore;
    private FloatingActionButton fab;
    private TextView mTvTotal,mTvMonthlyIncome,mTvMonthlyOutcome;

    private double netAssets;
    private double monthlyIncome,monthlyOutcome;

    public static Context getContext(){
        return context;
    }
    @RequiresApi(api = Build.VERSION_CODES.N)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        this.context = this;
//        binding = ActivityMainBinding.inflate(getLayoutInflater());
//        setContentView(binding.getRoot());


//        setSupportActionBar(binding.toolbar);

//        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment_content_main);
//        appBarConfiguration = new AppBarConfiguration.Builder(navController.getGraph()).build();
//        NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);

//        binding.fab.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
//                        .setAction("Action", null).show();
//            }
//        });

        initData();

        initView();
    }

    private void initData() {
        databaseHelper = DatabaseHelper.getDefaultDatabaseHelper(context);
    }

    @RequiresApi(api = Build.VERSION_CODES.N)
    @Override
    protected void onResume() {
        super.onResume();
        updateData();
    }

    @RequiresApi(api = Build.VERSION_CODES.N)
    private void updateData() {
        try{
            netAssets = databaseHelper.getNetAssets();
            mTvTotal.setText(new Double(netAssets).toString());
            DateFormat df = new SimpleDateFormat("yyyy_MM_dd");
            String date = df.format(Calendar.getInstance().getTime());
            String[] dateSplit = date.split("_");
            String year = dateSplit[0];
            String month = dateSplit[1];
            monthlyIncome = databaseHelper.getMonthlyIncome(year,month);
            mTvMonthlyIncome.setText(new Double(monthlyIncome).toString());
            monthlyOutcome = databaseHelper.getMonthlyOutcome(year,month);
            mTvMonthlyOutcome.setText(new Double(monthlyOutcome).toString());
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @RequiresApi(api = Build.VERSION_CODES.N)
    private void initView() {
        mDrawerLayout = findViewById(R.id.main_drawer);
        mTbMain = findViewById(R.id.toolbar_main);
        mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, mTbMain, R.string.open, R.string.close) {
            @Override
            public void onDrawerOpened(View drawerView) {
                super.onDrawerOpened(drawerView);
                invalidateOptionsMenu();
            }

            @Override
            public void onDrawerClosed(View drawerView) {
                super.onDrawerClosed(drawerView);
                invalidateOptionsMenu();
            }
        };
        mDrawerToggle.syncState();
        mDrawerLayout.setDrawerListener(mDrawerToggle);//设置监听器

        mLvSettings = findViewById(R.id.lv_settings);
        LvSettingsAdapter listViewSettingsAdapter = new LvSettingsAdapter();
        mLvSettings.setAdapter(listViewSettingsAdapter);
        mLvSettings.setOnItemClickListener(listViewSettingsAdapter);

        mLvMore = findViewById(R.id.lv_more);
        LvMoreAdapter listViewMoreAdapter = new LvMoreAdapter();
        mLvMore.setAdapter(listViewMoreAdapter);
        mLvMore.setOnItemClickListener(listViewMoreAdapter);

        fab = findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SharedPreferences.Editor editor = getSharedPreferences(MyConst.SETTINGS,MODE_PRIVATE).edit();
                editor.putString(MyConst.SETTINGS_STATE,MyConst.SETTINGS_STATE_ADD);
                editor.commit();
                Intent intent = new Intent(MainActivity.this,AddFlowActivity.class);
                startActivity(intent);
            }
        });

        RecyclerView rvDailyFlow = findViewById(R.id.rv_daily_flow);
        RvFlowAdapter rvFlowAdapter = new RvFlowAdapter(databaseHelper.getTotalDailyFlow(),databaseHelper.getDialyStatisticsList());
        // 设置布局管理器
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        rvDailyFlow.setLayoutManager(linearLayoutManager);
        // 设置adapter
        rvDailyFlow.setAdapter(rvFlowAdapter);
        mTvTotal = findViewById(R.id.tv_main_total_num);
        mTvMonthlyIncome = findViewById(R.id.tv_month_income_num);
        mTvMonthlyOutcome = findViewById(R.id.tv_month_outcome_num);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    public boolean onSupportNavigateUp() {
        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment_content_main);
        return NavigationUI.navigateUp(navController, appBarConfiguration)
                || super.onSupportNavigateUp();
    }
}